﻿namespace dotnet_notes.Tools
{
    /// <summary>
    /// 字符串工具类
    /// </summary>
    public static class StringTool
    {
        /// <summary>
        /// 对字符串脱敏
        /// </summary>
        /// <param name="input">要脱敏的字符串</param>
        /// <returns></returns>
        public static string Desensitize(string? input)
        {
            if (string.IsNullOrWhiteSpace(input)) return input;

            if (input.Length <= 1) return input;
            if (input.Length == 2) return input[0] + "*";

            int replaceLength = (int)Math.Ceiling(input.Length * 0.3);
            int startIndex = input.Length / 2 - replaceLength / 2;
            int endIndex = startIndex + replaceLength - 1;

            var sb = new StringBuilder(input);
            for (int i = startIndex; i <= endIndex; i++)
            {
                sb[i] = '*';
            }

            return sb.ToString();
        }
    }
}
